package com.hqq.concurrency.semaphone;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Customer
 * Created by heqianqian on 2017/7/30.
 */
public class Customer extends Thread {

    private static final Logger LOGGER = LoggerFactory.getLogger(Customer.class);

    private String name;

    private FruitShop fruitShop;

    private FruitBowl fruitBowl;

    public Customer(String name, FruitShop fruitShop) {
        this.name = name;
        this.fruitShop = fruitShop;
        this.fruitBowl = new FruitBowl();
    }

    @Override
    public void run() {
        while (fruitShop.countFruit() > 0) {
            FruitBowl bowl = fruitShop.takeBowl();
            Fruit fruit;

            if (bowl != null && (fruit = bowl.get()) != null) {
                LOGGER.info("{} took an {}", name, fruit);
                fruitBowl.put(fruit);
                fruitShop.returnBowl(bowl);
            }
        }

        LOGGER.info("{} took {}", name, fruitBowl);
    }
}
